package com.example.phonestore.service.impl;

import com.example.phonestore.entity.User;
import com.example.phonestore.mapper.UserMapper;
import com.example.phonestore.service.UserService;
import com.example.phonestore.util.JsonResult;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.example.phonestore.util.JsonResult.fail;
import static com.example.phonestore.util.JsonResult.success;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public JsonResult login(String username, String password,HttpSession session) {
        JsonResult jsonResult=new JsonResult();
        User result=userMapper.findByUsername(username);
        if (result==null){
            System.out.println("没有该用户");
            jsonResult.setMsg("没有该用户");
            return jsonResult;
        }
        String p=result.getPassword();
        if (!password.equals(p)){
            System.out.println("密码错误");
            jsonResult.setMsg("密码错误");
            return jsonResult;
        }
        session.setAttribute("uid",result.getUid());
        session.setAttribute("username",result.getUsername());
        return success();
    }

    @Override
    public JsonResult register(String username, String password) {
        JsonResult jsonResult=new JsonResult();
        User r=userMapper.findByUsername(username);
        if (r!=null){
            System.out.println("该用户已存在");
            jsonResult.setMsg("该用户已存在");
            return jsonResult;
        }
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        Integer i=userMapper.insertUser(user);
        if (i==1){
            return success();
        }
        jsonResult.setMsg("注册失败！");
        return jsonResult;
    }

    @Override
    public JsonResult updatePassword(Integer uid, String oldPassword, String newPassword) {
        User user=userMapper.findByUid(uid);
        JsonResult jsonResult=new JsonResult();
        if (!user.getPassword().equals(oldPassword)){
            System.out.println("原始密码错误！");
            jsonResult.setMsg("原始密码错误！");
            return jsonResult;
        }
        Integer i=userMapper.updatePassword(uid,newPassword);
        if (i!=1){
            System.out.println("修改失败！");
            jsonResult.setMsg("修改失败！");
            return jsonResult;
        }
        return success();
    }


    @Override
    public User showUser(Integer uid) {
        User user=userMapper.findByUid(uid);

        User user1=new User();
        user1.setUsername(user.getUsername());
        user1.setPhone(user.getPhone());
        user1.setEmail(user.getEmail());
        user1.setGender(user.getGender());
        return user1;
    }
    @Override
    public JsonResult updateUser(User user) {
        JsonResult jsonResult=new JsonResult();
        Integer j=userMapper.updateUser(user);
        if (j!=1){
            System.out.println("修改用户数据失败");
            jsonResult.setMsg("修改用户数据失败");
            return jsonResult;
        }
        return JsonResult.success();
    }




}
